package com.example.syyao.myapp.Sign;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.syyao.myapp.R;
import com.example.syyao.myapp.Response.Get_response;
import com.example.syyao.myapp.WebContact.JsonUtil;
import com.example.syyao.myapp.WebContact.WebAccessTools;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class Sign_up extends AppCompatActivity {

    Get_response sign_up_response;
    TextView sign_in;
    Button sign_up;
    TextView sign_identify;
    TextView username;
    TextView password;
    TextView identify;
    public String idnumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_up);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        identify = findViewById(R.id.sign_up_vcode);
        username = findViewById(R.id.sign_up_phone);
        password = findViewById(R.id.sign_up_password);

        sign_in=findViewById(R.id.sign_in_in_sign_up);
        sign_in.setOnClickListener(new sign_in_click());

        sign_identify = findViewById(R.id.sendVcode);
        sign_identify.setOnClickListener(new sign_identify());

        sign_up = findViewById(R.id.button_sign_up);
        sign_up.setOnClickListener(new sign_up());
    }

    private class sign_in_click implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent();
            intent.setClass(Sign_up.this, Sign_in.class);
            startActivity(intent);
            finish();
        }
    }
    private class sign_up implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            if (username.getText().toString().equals("")) {
                Toast.makeText(Sign_up.this, getResources().getString(R.string.username), Toast.LENGTH_SHORT).show();
                return;
            }
            if (password.getText().toString().equals("")) {
                Toast.makeText(Sign_up.this, getResources().getString(R.string.password), Toast.LENGTH_SHORT).show();
                return;
            }
            if(identify.getText().toString().equals("")){
                Toast.makeText(Sign_up.this, getResources().getString(R.string.identify), Toast.LENGTH_SHORT).show();
                return;
            }
            register(username.getText().toString(),password.getText().toString(),identify.getText().toString());
        }
    }

    private void register(final String username, final String password, final String text){
        if(!text.equals(idnumber)){
            Toast.makeText(Sign_up.this, "验证码错误！",Toast.LENGTH_SHORT).show();
            return;
        }
        new Thread(new Runnable() {

            @Override
            public void run() {

                String webContent = WebAccessTools.register(username,password);
                System.out.println(webContent);
                sign_up_response= (Get_response) JsonUtil.jsonToObject(webContent,
                        Get_response.class);
                handl.sendEmptyMessage(0x123);
            }
        }).start();

    }
    @SuppressLint("HandlerLeak")
    private Handler handl = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (sign_up_response != null && sign_up_response.getMessage().equals("成功")) {
                Toast.makeText(Sign_up.this, "注册成功！",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(Sign_up.this,Sign_in.class);
                startActivity(intent);
            } else {
                Toast.makeText(Sign_up.this, "发送失败！",Toast.LENGTH_SHORT).show();

            }
        };
    };
        public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
                Map<String, Object> map = new HashMap<>();
                 Class<?> clazz = obj.getClass();
                 System.out.println(clazz);
                 for (Field field : clazz.getDeclaredFields()) {
                         field.setAccessible(true);
                         String fieldName = field.getName();
                         Object value = field.get(obj);
                         map.put(fieldName, value);
                     }
                return map;
        }



    private class sign_identify implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            if(username.getText().toString().equals("")){
                Toast.makeText(Sign_up.this, getResources().getString(R.string.username), Toast.LENGTH_SHORT).show();
                return;
            }
            yanzheng(username.getText().toString());
        }
    }

    public void yanzheng(final String text){
        new Thread(new Runnable() {

            @Override
            public void run() {

                String webContent = WebAccessTools.identify(text);
                System.out.println("web:"+webContent);
                sign_up_response= (Get_response) JsonUtil.jsonToObject(webContent,
                        Get_response.class);
                handler.sendEmptyMessage(0x123);
            }
        }).start();
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (sign_up_response != null && sign_up_response.getMessage().equals("成功")) {
                Toast.makeText(Sign_up.this, "发送成功！",Toast.LENGTH_SHORT).show();
                idnumber = sign_up_response.getData().get("number").toString();
            } else {
                Toast.makeText(Sign_up.this, "发送失败！",Toast.LENGTH_SHORT).show();
            }
        };
    };
}
